<template>
    <div>
        <input type="file" @change="handleFileUpload" accept=".txt" />
        <button @click="downloadExcel">下载Excel</button>
    </div>
</template>

<script>
import XLSX from "xlsx";

export default {
    data() {
        return {
            phoneNumbers: [],
        };
    },
    methods: {
        handleFileUpload(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    const content = e.target.result;
                    this.phoneNumbers = content
                        .split("\n")
                        .map((line) => line.trim())
                        .filter((line) => line);
                };
                reader.readAsText(file);
            }
        },
        downloadExcel() {
            const worksheet = XLSX.utils.json_to_sheet(
                this.phoneNumbers.map((phone) => ({ 手机号码: phone }))
            );
            const workbook = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
            XLSX.writeFile(workbook, "phone_numbers.xlsx");
        },
    },
};
</script>
